package com.example.agboxdemo.controller;

import com.example.agboxdemo.entity.vo.ResultVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hnn
 * @version 1.0
 * @date 2025-07-07 15:07
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class VillageController {

    public static long count = 0;

    private final ObjectMapper objectMapper;

    @PostMapping("/village")
    public ResultVO work(@RequestParam("key") String key, @RequestParam("json") String json) {
        log.info("recv key:{},json:{}", key, json);
        ObjectNode on = objectMapper.createObjectNode();
        try {
            JsonNode jsonNode = objectMapper.readTree(json);
            String method = jsonNode.get("method").asText();
            JsonNode params = jsonNode.get("params");
            switch (method) {
                case "getList":
                    log.info("getGisType params:{}", params);
                    String v = "{\"page\":1,\"page_size\":10,\"total_page\":1,\"count\":1,\"villageList\":[{\"villageCode\":\"195046ec-60aa-4216-b254-cf7ff736584c\",\"villageName\":\"测试地块1\",\"address\":\"水电路100号\",\"updateTime\":\"2018-04-19 00:00:00\"}]}";
                    on = objectMapper.readValue(v, ObjectNode.class);
                    break;
                case "getBuilding":
                    log.info("getBuilding params:{}", params);
                    if ("195046ec-60aa-4216-b254-cf7ff736584c".equals(params.get("villageCode").asText())) {
                        String v2 = "{\"page\":1,\"page_size\":4,\"total_page\":1,\"count\":2,\"villageCode\":\"195046ec-60aa-4216-b254-cf7ff736584c\",\"buildingList\":[{\"buildingCode\":\"e3458009-4dfd-48d8-99f5-b7d87fb4f574\",\"buildingNo\":\"楼 A\",\"floorTotal\":5,\"houseTotal\":15,\"note\":\"楼 A\"},{\"buildingCode\":\"0535bfe0-235f-42c0-bd8c-5049f21a7070\",\"buildingNo\":\"A\",\"floorTotal\":5,\"houseTotal\":4,\"note\":\"描述 A14444\"}]}";
                        on = objectMapper.readValue(v2, ObjectNode.class);
                    }
                    break;
                case "getHouse":
                    log.info("getHouse params:{}", params);
                    if ("e3458009-4dfd-48d8-99f5-b7d87fb4f574".equals(params.get("buildingCode").asText())) {
                        String v3 = "{\"page\":1,\"pageSize\":4,\"totalPage\":1,\"count\":2,\"buildingCode\":\"e3458009-4dfd-48d8-99f5-b7d87fb4f574\",\"houseList\":[{\"houseCode\":\"8d96dba6-5837-4ca9-96b9-bfb4d77f1d41\",\"floor\":3,\"houseNo\":\"301\",\"houseLabelCode\":0,\"houseLabel\":\"\",\"housePurposeCode\":0,\"housePurpose\":\"\",\"note\":\"\",\"isPublic\":false,\"isAction\":false,\"tags\":[{\"tag\":\"视频安防监控\",\"systemTypeCode\":1002},{\"tag\":\"出入门禁控制\",\"systemTypeCode\":1005}]},{\"houseCode\":\"e6ebcd3d-2e22-498f-a052-75e76cfb99bd\",\"floor\":3,\"houseNo\":\"303\",\"houseLabelCode\":0,\"houseLabel\":\"\",\"housePurposeCode\":0,\"housePurpose\":\"\",\"note\":\"\",\"isPublic\":false,\"isAction\":false,\"tags\":[{\"tag\":\"出入门禁控制\",\"systemTypeCode\":1005}]}]}";
                        on = objectMapper.readValue(v3, ObjectNode.class);
                    }
                    break;
                case "getPerson":
                    log.info("getPerson params:{}", params);
                    String v4 = "{\"count\":2,\"page\":1,\"totalPage\":1,\"pageSize\":20,\"person\":[{\"credentialType\":111,\"credentialNo\":\"310102198403111636\",\"name\":\"王二\",\"facePicUrl\":{\"url\":\"http://192.168.36.57/\",\"path\":\"/upload/idPic/202211/6d21bf6f75ee4054857bdb43928e6294.jpeg\"},\"phone1\":\"139119191\",\"phone2\":\"13819191919\",\"phone3\":\"\",\"personTypeCode\":2,\"personEntranceTypeCode\":1,\"cardNumber\":\"22233444455\",\"personCode\":\"9ac2ccf2-209a-4e05-9436-8d033628c4b3\",\"updateTime\":\"2022-11-25 12:07:43\"},{\"credentialType\":131,\"credentialNo\":\"887788\",\"name\":\"王麻子\",\"facePicUrl\":{\"url\":\"http://192.168.36.57/\",\"path\":\"/upload/idPic/202211/b754ad95bbde4c989a72083bfbc809a3.jpeg\"},\"phone1\":\"\",\"phone2\":\"136777888811\",\"phone3\":\"\",\"personTypeCode\":1,\"personEntranceTypeCode\":806,\"cardNumber\":\"\",\"personCode\":\"52e90ced-08d4-49af-81d7-905103ba7e69\",\"updateTime\":\"2022-11-24 18:45:43\"}]}";
                    on = objectMapper.readValue(v4, ObjectNode.class);
                    break;
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

//        on.put("datetime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return new ResultVO<>(on);
    }
}
